package com.sq.partner.manager.util;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import com.sq.partner.manager.common.exception.AppException;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtil {
	private static final Logger logger =LogUtil.getLog(RedisUtil.class);
    private static String hostAndPorts="10.196.21.131:6379&10.196.21.132:6379&10.196.21.133:6379&10.196.21.131:6380";
    private static String redisPwd = "cicagri";
	private static RedisUtil instance = null;
	private static String key = "Sales-Agent:";
	private JedisCluster jedis= null;
	
	public static RedisUtil getInstance() {
		if (instance == null) {
			instance = new RedisUtil();
		}
		return instance;
	}

	private RedisUtil() {
	        // 添加集群的服务节点Set集合
			String[] split = hostAndPorts.split("&");
	        Set<HostAndPort> nodes = new HashSet<HostAndPort>();
			for (String str : split) {
				nodes.add(new HostAndPort(str.split(":")[0],Integer.valueOf(str.split(":")[1])));
			}
			JedisPoolConfig config = new JedisPoolConfig();
			config.setTestWhileIdle(true);
			config.setMinEvictableIdleTimeMillis(60000);
			config.setTimeBetweenEvictionRunsMillis(30000);
			config.setNumTestsPerEvictionRun(-1);
			config.setMinEvictableIdleTimeMillis(60000);
			jedis = new JedisCluster(nodes,60000,60000,100,redisPwd,config);
	}
	
	
	 public Map<String,String> getValue(String agentNo) throws AppException{
			try {
				if (StrUtil.isNotEmpty(key)) {
					Map<String, String> map = jedis.hgetAll(key+agentNo);
					logger.info("销管系统返回数据————————————————"+map);
					if(null != map && map.size()!=0){
						return map;
					}
				}
			} catch (Exception e) {
				logger.info(e);
				throw new AppException("请求销管查询数据失败");
			}
		return null;
	 }
}
